// Test.cpp : Defines the entry point for the console application.
//

#include "StdAfx.h"
#include <stdio.h>
#include "winsock2.h"

void main() {
	
	WSADATA wsaData;
	SOCKET RecvSocket;
	sockaddr_in RecvAddr;
	int Port = 8082;
	char RecvBuf[1024];
	char SendBuf[1024];
	int  BufLen = 1024;
	sockaddr_in SenderAddr;
	int SenderAddrSize = sizeof(SenderAddr);

	strcpy(SendBuf, "abcdefefdfdfd");
	
	//-----------------------------------------------
	// Initialize Winsock
	WSAStartup(MAKEWORD(2,2), &wsaData);
	
	//-----------------------------------------------
	// Create a receiver socket to receive datagrams
	RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	
	//-----------------------------------------------
	// Bind the socket to any address and the specified port.
	RecvAddr.sin_family = AF_INET;
	RecvAddr.sin_port = htons(Port);
	RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);
	
	bind(RecvSocket, (SOCKADDR *) &RecvAddr, sizeof(RecvAddr));
	
	int toPort = 8081;
	SenderAddr.sin_family = AF_INET;
	SenderAddr.sin_port = htons(toPort);
	SenderAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
	printf("Sending datagrams...\n");
	sendto(RecvSocket, 
		SendBuf, 
		BufLen, 
		0, 
		(SOCKADDR *)&SenderAddr, 
		SenderAddrSize);
	
	//-----------------------------------------------
	// Call the recvfrom function to receive datagrams
	// on the bound socket.
	printf("Receiving datagrams...\n");
	recvfrom(RecvSocket, 
		RecvBuf, 
		BufLen, 
		0, 
		(SOCKADDR *)&SenderAddr, 
		&SenderAddrSize);
	
	printf("Recved:%s\n", RecvBuf);
	//-----------------------------------------------
	// Close the socket when finished receiving datagrams
	printf("Finished receiving. Closing socket.\n");
	closesocket(RecvSocket);
	
	//-----------------------------------------------
	// Clean up and exit.
	printf("Exiting.\n");
	WSACleanup();
	return;
}

